using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.SqlManagerUI.Scripting;
using Microsoft.SqlServer.Management.UI;
using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class SelectServerWizardPage : WizardPage
	{
		private GroupBox grbAuthentication;

		private RadioButton rdbSqlAuth;

		private Panel pnlServerInfo;

		private TextBox txtServerName;

		private Label lblServer;

		private RadioButton rdbProvideConnectionStr;

		private RadioButton rdbSpecifyServer;

		private TextBox txtPassword;

		private Label lblPassword;

		private TextBox txtUsername;

		private Label lblUsername;

		private TextBox txtConnectionString;

		private Label lblConnectionString;

		private Panel pnlSqlAuth;

		private RadioButton rdbWinAuth;

		private Panel pnlConnStr;

		private Panel pnlServer;

		private GenScriptWizForm _parent;

		private GenWizGeneralData _generalData;

		private readonly string LOCAL_SERVER_NAME = "localhost";

		internal string ConnectionString
		{
			set
			{
				if (value == null)
				{
					this.rdbProvideConnectionStr.Checked = false;
					this.rdbSpecifyServer.Checked = true;
				}
				else
				{
					this.rdbSpecifyServer.Checked = false;
					this.rdbProvideConnectionStr.Checked = true;
				}
				this.txtConnectionString.Text = value;
			}
		}

		public SelectServerWizardPage(WizardForm wizard) : base(wizard)
		{
			this.InitializeComponent();
			this.LoadStrings();
			this._parent = (GenScriptWizForm)wizard;
			base.PageID = 0;
			this.SubTitle = GenScriptWizSR.GenScriptSelectServerStepDesc;
			this.Title = GenScriptWizSR.GenScriptSelectServerStepTitle;
			this._generalData = (GenWizGeneralData)base.WizardInputs.FindItem(0);
		}

		protected override void OnInitializePage(EventArgs e)
		{
			this.Wizard.BackEnabled = true;
			this.rdbSpecifyServer.Checked = true;
			this.txtServerName.Text = this.LOCAL_SERVER_NAME;
			this.rdbWinAuth.Checked = true;
		}

		protected override void OnEnterPage(EventArgs e)
		{
			this.Wizard.NextEnabled = true;
			this.Wizard.FinishEnabled = false;
		}

		protected override void OnLeavePage(LeavePageEventArgs e)
		{
			if (e.Button == WizardButton.Next || e.Button == WizardButton.Finish)
			{
				this._generalData.SaveFileName = "";
				if (!this.ValidateForm())
				{
					e.Cancel = true;
					return;
				}
				if (!string.IsNullOrEmpty(this._generalData.DatabaseName))
				{
					base.WizardInputs.AddEditItem(1, new GenWizTransferOptions(this._generalData.SqlServerVersion));
					base.WizardInputs.AddEditItem(3, true);
				}
			}
		}

		private void InitializeComponent()
		{
			this.rdbWinAuth = new RadioButton();
			this.grbAuthentication = new GroupBox();
			this.pnlSqlAuth = new Panel();
			this.lblUsername = new Label();
			this.txtUsername = new TextBox();
			this.txtPassword = new TextBox();
			this.lblPassword = new Label();
			this.rdbSqlAuth = new RadioButton();
			this.pnlServerInfo = new Panel();
			this.pnlConnStr = new Panel();
			this.txtConnectionString = new TextBox();
			this.lblConnectionString = new Label();
			this.rdbProvideConnectionStr = new RadioButton();
			this.pnlServer = new Panel();
			this.txtServerName = new TextBox();
			this.lblServer = new Label();
			this.rdbSpecifyServer = new RadioButton();
			this.grbAuthentication.SuspendLayout();
			this.pnlSqlAuth.SuspendLayout();
			this.pnlServerInfo.SuspendLayout();
			this.pnlConnStr.SuspendLayout();
			this.pnlServer.SuspendLayout();
			base.SuspendLayout();
			this.rdbWinAuth.AutoSize = true;
			this.rdbWinAuth.Location = new Point(6, 16);
			this.rdbWinAuth.Name = "rdbWinAuth";
			this.rdbWinAuth.Size = new Size(146, 17);
			this.rdbWinAuth.TabIndex = 2;
			this.rdbWinAuth.TabStop = true;
			this.rdbWinAuth.Text = GenScriptWizSR.SpecifyServerInfo;
			this.rdbWinAuth.UseVisualStyleBackColor = true;
			this.rdbWinAuth.CheckedChanged += new EventHandler(this.rdbWinAuth_CheckedChanged);
			this.grbAuthentication.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.grbAuthentication.Controls.Add(this.pnlSqlAuth);
			this.grbAuthentication.Controls.Add(this.rdbWinAuth);
			this.grbAuthentication.Controls.Add(this.rdbSqlAuth);
			this.grbAuthentication.Location = new Point(42, 31);
			this.grbAuthentication.Name = "grbAuthentication";
			this.grbAuthentication.Size = new Size(387, 111);
			this.grbAuthentication.TabIndex = 1;
			this.grbAuthentication.TabStop = false;
			this.grbAuthentication.Text = GenScriptWizSR.AuthGroupBox;
			this.pnlSqlAuth.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlSqlAuth.Controls.Add(this.lblUsername);
			this.pnlSqlAuth.Controls.Add(this.txtUsername);
			this.pnlSqlAuth.Controls.Add(this.txtPassword);
			this.pnlSqlAuth.Controls.Add(this.lblPassword);
			this.pnlSqlAuth.Location = new Point(6, 54);
			this.pnlSqlAuth.Name = "pnlSqlAuth";
			this.pnlSqlAuth.Size = new Size(375, 53);
			this.pnlSqlAuth.TabIndex = 4;
			this.lblUsername.AutoSize = true;
			this.lblUsername.Location = new Point(21, 5);
			this.lblUsername.Name = "lblUsername";
			this.lblUsername.Size = new Size(55, 13);
			this.lblUsername.TabIndex = 2;
			this.lblUsername.Text = GenScriptWizSR.SqlUsername;
			this.txtUsername.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtUsername.Location = new Point(148, 2);
			this.txtUsername.MaxLength = 128;
			this.txtUsername.Name = "txtUsername";
			this.txtUsername.Size = new Size(215, 20);
			this.txtUsername.TabIndex = 4;
			this.txtPassword.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtPassword.Location = new Point(148, 28);
			this.txtPassword.MaxLength = 128;
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new Size(215, 20);
			this.txtPassword.TabIndex = 5;
			this.lblPassword.AutoSize = true;
			this.lblPassword.Location = new Point(23, 31);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new Size(53, 13);
			this.lblPassword.TabIndex = 2;
			this.lblPassword.Text = GenScriptWizSR.SqlPassword;
			this.rdbSqlAuth.AutoSize = true;
			this.rdbSqlAuth.Location = new Point(6, 35);
			this.rdbSqlAuth.Name = "rdbSqlAuth";
			this.rdbSqlAuth.Size = new Size(172, 17);
			this.rdbSqlAuth.TabIndex = 3;
			this.rdbSqlAuth.TabStop = true;
			this.rdbSqlAuth.Text = GenScriptWizSR.SQLAuth;
			this.rdbSqlAuth.UseVisualStyleBackColor = true;
			this.rdbSqlAuth.CheckedChanged += new EventHandler(this.rdbSqlAuth_CheckedChanged);
			this.pnlServerInfo.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlServerInfo.Controls.Add(this.pnlConnStr);
			this.pnlServerInfo.Controls.Add(this.rdbProvideConnectionStr);
			this.pnlServerInfo.Controls.Add(this.pnlServer);
			this.pnlServerInfo.Controls.Add(this.rdbSpecifyServer);
			this.pnlServerInfo.Location = new Point(6, 0);
			this.pnlServerInfo.Name = "pnlServerInfo";
			this.pnlServerInfo.Size = new Size(456, 264);
			this.pnlServerInfo.TabIndex = 2;
			this.pnlConnStr.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlConnStr.Controls.Add(this.txtConnectionString);
			this.pnlConnStr.Controls.Add(this.lblConnectionString);
			this.pnlConnStr.Location = new Point(27, 209);
			this.pnlConnStr.Name = "pnlConnStr";
			this.pnlConnStr.Size = new Size(426, 46);
			this.pnlConnStr.TabIndex = 4;
			this.txtConnectionString.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtConnectionString.Location = new Point(18, 18);
			this.txtConnectionString.Name = "txtConnectionString";
			this.txtConnectionString.Size = new Size(387, 20);
			this.txtConnectionString.TabIndex = 2;
			this.lblConnectionString.AutoSize = true;
			this.lblConnectionString.Location = new Point(15, 2);
			this.lblConnectionString.Name = "lblConnectionString";
			this.lblConnectionString.Size = new Size(89, 13);
			this.lblConnectionString.TabIndex = 2;
			this.lblConnectionString.Text = GenScriptWizSR.ConnectionString;
			this.rdbProvideConnectionStr.AutoSize = true;
			this.rdbProvideConnectionStr.Location = new Point(4, 191);
			this.rdbProvideConnectionStr.Name = "rdbProvideConnectionStr";
			this.rdbProvideConnectionStr.Size = new Size(145, 17);
			this.rdbProvideConnectionStr.TabIndex = 1;
			this.rdbProvideConnectionStr.TabStop = true;
			this.rdbProvideConnectionStr.Text = GenScriptWizSR.UseConnectionStr;
			this.rdbProvideConnectionStr.UseVisualStyleBackColor = true;
			this.rdbProvideConnectionStr.CheckedChanged += new EventHandler(this.rdbProvideConnectionStr_CheckedChanged);
			this.pnlServer.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlServer.Controls.Add(this.txtServerName);
			this.pnlServer.Controls.Add(this.grbAuthentication);
			this.pnlServer.Controls.Add(this.lblServer);
			this.pnlServer.Location = new Point(3, 24);
			this.pnlServer.Name = "pnlServer";
			this.pnlServer.Size = new Size(450, 161);
			this.pnlServer.TabIndex = 5;
			this.txtServerName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtServerName.Location = new Point(149, 5);
			this.txtServerName.MaxLength = 128;
			this.txtServerName.Name = "txtServerName";
			this.txtServerName.Size = new Size(280, 20);
			this.txtServerName.TabIndex = 1;
			this.lblServer.AutoSize = true;
			this.lblServer.Location = new Point(39, 5);
			this.lblServer.Name = "lblServer";
			this.lblServer.Size = new Size(38, 13);
			this.lblServer.TabIndex = 2;
			this.lblServer.Text = GenScriptWizSR.ServerName;
			this.rdbSpecifyServer.AutoSize = true;
			this.rdbSpecifyServer.Location = new Point(6, 5);
			this.rdbSpecifyServer.Name = "rdbSpecifyServer";
			this.rdbSpecifyServer.Size = new Size(146, 17);
			this.rdbSpecifyServer.TabIndex = 0;
			this.rdbSpecifyServer.TabStop = true;
			this.rdbSpecifyServer.Text = GenScriptWizSR.SpecifyServerInfo;
			this.rdbSpecifyServer.UseVisualStyleBackColor = true;
			this.rdbSpecifyServer.CheckedChanged += new EventHandler(this.rdbSpecifyServer_CheckedChanged);
			base.Controls.Add(this.pnlServerInfo);
			base.Name = "SelectServerWizardPage";
			base.Size = new Size(468, 275);
			this.grbAuthentication.ResumeLayout(false);
			this.grbAuthentication.PerformLayout();
			this.pnlSqlAuth.ResumeLayout(false);
			this.pnlSqlAuth.PerformLayout();
			this.pnlServerInfo.ResumeLayout(false);
			this.pnlServerInfo.PerformLayout();
			this.pnlConnStr.ResumeLayout(false);
			this.pnlConnStr.PerformLayout();
			this.pnlServer.ResumeLayout(false);
			this.pnlServer.PerformLayout();
			base.ResumeLayout(false);
		}

		internal bool ValidateForm()
		{
			string serverName = null;
			string text = null;
			string text2 = null;
			try
			{
				this._generalData.ConnectionStringDB = false;
				if (this.rdbProvideConnectionStr.Checked)
				{
					try
					{
						if (string.IsNullOrEmpty(this.txtConnectionString.Text))
						{
							this._parent.MessageProvider.ShowMessage(GenScriptWizSR.InvalidConnString, GenScriptWizSR.WizardText(""), ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this);
							bool result = false;
							return result;
						}
						SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(this.txtConnectionString.Text);
						serverName = sqlConnectionStringBuilder.DataSource;
						if (!string.IsNullOrEmpty(sqlConnectionStringBuilder.UserID))
						{
							text2 = sqlConnectionStringBuilder.UserID;
						}
						if (!string.IsNullOrEmpty(sqlConnectionStringBuilder.Password))
						{
							text = sqlConnectionStringBuilder.Password;
						}
						this._generalData.ScriptingDriver = new ScriptingDriver(this.txtConnectionString.Text);
						if (!string.IsNullOrEmpty(sqlConnectionStringBuilder.InitialCatalog))
						{
							this._generalData.DatabaseName = sqlConnectionStringBuilder.InitialCatalog;
							this._generalData.ConnectionStringDB = true;
						}
						if (!string.IsNullOrEmpty(sqlConnectionStringBuilder.AttachDBFilename) && sqlConnectionStringBuilder.UserInstance)
						{
							if (!string.IsNullOrEmpty(this._generalData.ScriptingDriver.DatabaseName))
							{
								this._generalData.DatabaseName = this._generalData.ScriptingDriver.DatabaseName;
							}
							else
							{
								this._generalData.DatabaseName = sqlConnectionStringBuilder.AttachDBFilename;
							}
							this._generalData.ConnectionStringDB = true;
						}
						goto IL_232;
					}
					catch (ArgumentException)
					{
						this._parent.MessageProvider.ShowMessage(new ArgumentException(ScriptingEngineResources.ERROR_InvalidConnectionString), null, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this);
						bool result = false;
						return result;
					}
				}
				if (!string.IsNullOrEmpty(this.txtServerName.Text))
				{
					serverName = this.txtServerName.Text.Trim();
				}
				else
				{
					serverName = this.LOCAL_SERVER_NAME;
				}
				if (this.rdbSqlAuth.Checked)
				{
					text2 = this.txtUsername.Text;
					text = this.txtPassword.Text;
					if (string.IsNullOrEmpty(text2) || string.IsNullOrEmpty(text))
					{
						this._parent.MessageProvider.ShowMessage(GenScriptWizSR.EmptyUserAndPasswordMessageText, GenScriptWizSR.EmptyUserAndPasswordMessageTitle, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this);
						bool result = false;
						return result;
					}
					this._generalData.ScriptingDriver = new ScriptingDriver("", serverName, text2, text);
				}
				else
				{
					text2 = null;
					text = null;
					this._generalData.ScriptingDriver = new ScriptingDriver("", serverName);
				}
				IL_232:
				if (!string.IsNullOrEmpty(text2) && !string.IsNullOrEmpty(text))
				{
					this._generalData.ConnectionInfo = new SqlConnectionInfo(serverName, text2, text);
				}
				else
				{
					this._generalData.ConnectionInfo = new SqlConnectionInfo(serverName);
				}
			}
			catch (Exception e)
			{
				this._parent.MessageProvider.ShowMessage(e, GenScriptWizSR.WizardText(""), ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this);
				bool result = false;
				return result;
			}
			return true;
		}

		private void LoadStrings()
		{
			this.rdbSpecifyServer.Text = GenScriptWizSR.SpecifyServerInfo;
			this.lblServer.Text = GenScriptWizSR.ServerName;
			this.grbAuthentication.Text = GenScriptWizSR.AuthGroupBox;
			this.rdbWinAuth.Text = GenScriptWizSR.WindowsAuth;
			this.rdbSqlAuth.Text = GenScriptWizSR.SQLAuth;
			this.lblUsername.Text = GenScriptWizSR.SqlUsername;
			this.lblPassword.Text = GenScriptWizSR.SqlPassword;
			this.rdbProvideConnectionStr.Text = GenScriptWizSR.UseConnectionStr;
			this.lblConnectionString.Text = GenScriptWizSR.ConnectionString;
		}

		private void rdbSpecifyServer_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbSpecifyServer.Checked)
			{
				this.pnlConnStr.Enabled = false;
				this.pnlServer.Enabled = true;
			}
		}

		private void rdbProvideConnectionStr_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbProvideConnectionStr.Checked)
			{
				this.pnlConnStr.Enabled = true;
				this.pnlServer.Enabled = false;
			}
		}

		private void rdbWinAuth_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbWinAuth.Checked)
			{
				this.pnlSqlAuth.Enabled = false;
			}
		}

		private void rdbSqlAuth_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbSqlAuth.Checked)
			{
				this.pnlSqlAuth.Enabled = true;
			}
		}
	}
}
